package com.cs.beneficios.web.action;
import com.cs.beneficios.business.model.ValorFlexibleVO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.beneficios.common.entity.Consultas;
import com.cs.paoyer.administration.business.model.UserVO;

import java.io.IOException;

import java.sql.SQLException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class InsertarDominioAction extends Action
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
      //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	  ConnectionFactory cf = new ConnectionFactory();
      Consultas objCons = new Consultas(cf);
       try{
         HttpSession session = request.getSession();
         session.removeAttribute("listatam");
         
         //Proceso para la auditoria
          UserVO user = (UserVO) session.getAttribute("userInfo");
          String terminalAddress = request.getRemoteAddr();
          String strTerminal = ("WEB="+terminalAddress);
          Calendar c = Calendar.getInstance(); 
          SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");   
          //Fin del proceso
         String Escoger = (String)session.getAttribute("escoger");
         int opc=Integer.parseInt(Escoger.trim());
         
         String strCodAtri= request.getParameter("codatributo");
         
         
         String strTipoVflDescrip= request.getParameter("tipovfldescrip");
         String strTipoVflVigente= request.getParameter("tipovflvigente");
         
         String strTipoVflNombre = strTipoVflDescrip+strCodAtri;
         
         String strArrDo[] = {strTipoVflNombre,strTipoVflDescrip,strTipoVflVigente,
                              user.getName(), dateFormat.format(c.getTime()),strTerminal,
                              user.getName(),dateFormat.format(c.getTime()),strTerminal};
         
         int bInsertTipoVFL= objCons.InsertTipoVFL(strArrDo);
         
         int iCodigoMax = objCons.getCodigoTipoVFL();
         String strArrAct[] = {user.getName(), dateFormat.format(c.getTime()),strTerminal};
         int ActualizoBien= objCons.ActualizarAtributo(strCodAtri, iCodigoMax,strArrAct);
         
         // PROCESO PARA CALCULAR Y ACTUALIZA EL ATRIBUTO GENERICO
         int iCodAtriGenerico = objCons.ObtenerCodAtriEspe(strCodAtri);
         int ActualizoBienGene = objCons.ActualizarAtributoGene(iCodAtriGenerico, iCodigoMax,strArrAct);
         // FIN PROCESO
         
          for(int i=1;i<8;i++)
         {
            
            String strDescripcion= request.getParameter("descripcion"+i);
                   
            String Nombre = strDescripcion+strCodAtri;
            String strArr[] = {Nombre,strDescripcion, user.getName(),dateFormat.format(c.getTime()),
                               strTerminal,user.getName(),dateFormat.format(c.getTime()),strTerminal,null};
           
            if(!strDescripcion.equalsIgnoreCase(""))
            {
            
              int bInsertVFL= objCons.InsertValorVFL(strArr, iCodigoMax);
            }  
         }
         
         if (opc != 2)
         {
       
           String strCodigo=String.valueOf(iCodigoMax);
           
           Collection ValorFlexible = objCons.getDominio(strCodigo);
           Collection ListaTamano = new ArrayList();
           List lista = (List)ValorFlexible;
           ValorFlexibleVO objvfl = (ValorFlexibleVO)lista.get(0);
           ListaTamano.add(objvfl);
           
           session.setAttribute("listatam",ListaTamano);
           session.setAttribute("valorflex",ValorFlexible);
           cf.closeConnection(objCons.getConn());
           return mapping.findForward("success");
         }else
         {
           session.setAttribute("Error","0");
           cf.closeConnection(objCons.getConn());
           return mapping.findForward("modcorrecto");
         }
         
      }catch(SQLException e)
      {
		System.err.println("/******** TDSUB - cwsub");
		e.printStackTrace();
		System.err.println("TDSSPE - cwsub ********/");
      }finally
      {
        try{
          cf.closeConnection(objCons.getConn());
        }catch(SQLException e)
        {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSSPE - cwsub ********/");
        }
      }
      return mapping.findForward("modcorrecto");
  }
}